package com.cl.blog.service.impl;

/**
 * @Author 付
 * @Date 2024-03-28 16:48
 * @Version 1.0
 * @description
 * @since 1.0
 */

import com.cl.blog.document.MailDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class MailService {

	@Autowired
	JavaMailSender javaMailSender;

	@Value("${spring.mail.username}")
	private String username;

	// 使用 qqMailSender 或 ostMailSender 发送邮件的方法...
	public void ostSeander(MailDto mailDto) throws Exception {
// 创建一个邮件消息
		MimeMessage message = javaMailSender.createMimeMessage();

		// 创建 MimeMessageHelper
		MimeMessageHelper helper = new MimeMessageHelper(message, false);
		// 发件人邮箱和名称
		helper.setFrom(username);
		// 邮件标题
		helper.setSubject(mailDto.getSubject());
		// 邮件正文，第二个参数表示是否是HTML正文
		helper.setText("<p>"+mailDto.getContent()+"</p>", true);
		for (String toMail : mailDto.getTos()) {
			// 收件人邮箱
			helper.setTo(toMail);
			// 发送
			javaMailSender.send(message);
		}
	}
}
